﻿using Application.Abstractions.Messaging;
using Application.Todos.Complete;
using Mediator;
using SharedKernel;
using WebApi.Extensions;
using WebApi.Infrastructure;

namespace WebApi.Endpoints.Todos;

internal sealed class Complete : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPut("todos/{id:guid}/complete", async (
            Guid id,
            IMediator mediator,
            CancellationToken cancellationToken) =>
        {
            var command = new CompleteTodoCommand(id);
            Result result = await mediator.Send(command, cancellationToken);
            return result.Match(Results.NoContent, ApiResponse.Problem);
        })
        .WithTags(Tags.Todos)
        .HasApiVersion(1, 0)
        .RequireAuthorization();
    }
}
